<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2019/3/1
 * Time: 10:23
 */

namespace App\HttpController\Admin;


use App\Model\WechatsCustomModel;

class CustomController extends AuthController
{
    /**
     * 获取列表
     */
    public function index(){
        $request = $this->request();
        $applet = $request->getRequestParam('applet')??'';
        if(!$applet){
            return $this->writeJson(1,null,'参数不正确！');
        }
        $wechatModel = new WechatsCustomModel();
        $list = $wechatModel->where('applet',$applet)->get();
        return $this->writeJson(0,$list,'ok');
    }


    /**
     * 新增/更新
     */
    public function update(){
        $request = $this->request();
        $params = $request->getRequestParam();
        $wechatModel = new WechatsCustomModel();
        if(!$params['applet']){
            return $this->writeJson(1,null,'参数不正确！');
        }
        if($params['id']){
            //更新
            $idInfo = $wechatModel->find((int)$params['id']);
            if(!$idInfo){
                return $this->writeJson(1,null,'更新失败，数据不存在！');
            }
            if($wechatModel->update($params)){
                return $this->writeJson(0,null,'更新成功');
            }
        }else{
            //新增
            $idInfo = $wechatModel->create($params);
            if($idInfo){
                return $this->writeJson(0,null,'新增成功');
            }
        }
        return $this->writeJson(1,null,'系统出错了');
    }


    /**
     * 删除
     * @return bool
     * @throws \Throwable
     */
    public function destroy(){
        $request = $this->request();
        $id = $request->getRequestParam('id');
        $wechatModel = new WechatsCustomModel();
        $wechatModel->destroy($id);
        return $this->writeJson(0);
    }

}
